package com.simple.base.dao;

import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.springframework.beans.factory.annotation.Autowired;

import com.simple.portal.vo.LoggerVO;
import com.simple.util.common.DateUtil;

public abstract class BaseDAO extends AbstractBaseDAO{
	/**
	 * Sets the super session factory.
	 * 
	 * @param sessionFactory
	 *            the new super session factory
	 */
	@Autowired
	SessionFactory sessionFactory;
	
	protected SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
	/**
	 * get table name by Object
	 * @param object table corresponding to Object
	 * @return String table name 
	 */
	private String getTableName(Object object){
		AbstractEntityPersister classMetadata = (AbstractEntityPersister)sessionFactory.getClassMetadata(object.getClass()); 
		return classMetadata.getTableName();
	}
	/**
	 * record the logger when operating data
	 * @param operator
	 * @param entity
	 * @param tablePrimaryKey
	 */
	public void saveLogger(String operator,Object entity,String tablePrimaryKey,String type){
		LoggerVO loggerVO =new LoggerVO();
		loggerVO.setOperator(operator);
		loggerVO.setTableName(getTableName(entity));
		loggerVO.setTablePk(tablePrimaryKey);
		loggerVO.setOperateFlag(type);
		loggerVO.setOperateDate(DateUtil.getNowDate());
		save(loggerVO);
	}


}
